﻿//Author: Christian TQ
//License: MIT License
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using TQ.Sharepoint.Webparts;
using Microsoft.Office.Server.UserProfiles;
namespace TQ.Sharepoint.Webparts
{
    public static class MicrobloggerExtension
    {
        /// <summary>
        /// Converts url string to html anchor tag link
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string ToLink(this string url)
        {
            string http="http://";
            if(!url.Contains(http))
                url=http+url;
            return "<a href='" + url + "' alt='" + url + "' target=\"_blank\">link</a> ";
        }
        /// <summary>
        /// Converts image url to img tag
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string ToImage(this string url)
        {
            string img = "<span class=\"mb-divImage\"><img align=\"absmiddle\" class=\"mb-Image\" src=\"{0}\"></span>";
            string postImg=string.Empty;
            string file = Path.GetFileName(url);

            if ((url.Contains("www.") || url.Contains("http://")) && (file.Contains(".jpg") || file.Contains(".png") || file.Contains(".gif")))
            {
            
            postImg = string.Format(img, url);

            }else
            {
                postImg = "<a class='mb-invalid'>Invalid Image!</a> ";
            }
            return postImg;
        }

        /// <summary>
        /// Returns current profile working email address
        /// </summary>
        /// <param name="profile"></param>
        /// <returns></returns>
        public static string ToWorkEMail(this UserProfile profile)
        {
            return (profile["WorkEmail"].Value != null) ? profile["WorkEmail"].Value.ToString() : string.Empty;
        }

        /// <summary>
        /// Removes domain of user's display name
        /// </summary>
        /// <param name="displayName"></param>
        /// <returns></returns>
        public static string RemoveDomain(this string displayName)
        {

            string[] strArray = displayName.Split(new char[] { '\\' });
            return strArray[strArray.Length - 1];
        }

        /// <summary>
        /// Gets My Site Host URL From user's my Site profile URL
        /// </summary>
        /// <param name="mySiteURL"></param>
        /// <returns></returns>
        public static string GetMySiteHostUrl(this string mySiteURL)
        {
            return mySiteURL.Substring(0, mySiteURL.LastIndexOf("/"));
        }

        /// <summary>
        /// Resize profile picture thumbnail from medium to small
        /// </summary>
        /// <param name="picURL"></param>
        /// <returns></returns>
        public static string ResizeThumb(this string picURL)
        {
            return picURL.Replace("MThumb", "SThumb");
        }


        public static string GetElapsedTime(this DateTime date)
        {
            return ConvertTimeSpanAgo(DateTime.Now.Subtract(date));
        }

        /// <summary>
        /// used by GetElapsedTime method
        /// </summary>
        /// <param name="diffDate"></param>
        /// <returns></returns>
        private static string ConvertTimeSpanAgo(TimeSpan diffDate)
        {
            StringBuilder date = new StringBuilder();
            if (diffDate.Days == 1)
                date.Append("1 day ago");
            if (diffDate.Days > 0)
                date.AppendFormat("{0} days ago", diffDate.Days);
            else if (diffDate.Minutes == 1)
                date.Append("1 minute ago");
            else if (diffDate.Minutes > 0)
                date.AppendFormat("{0} minutes ago", diffDate.Minutes);
            else if (diffDate.Seconds == 1)
                date.Append("1 second ago");
            else if (diffDate.Seconds > 0)
                date.AppendFormat("{0} seconds ago", diffDate.Seconds);
            else if (diffDate.Milliseconds > 0)
                date.AppendFormat("a moment ago");
            return date.ToString();
        }

        public static int ToNegative(this int num)
        {
            return num * -1;
        }
    }
}
